﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using VersionComparer.Configuration;

namespace VersionComparer.DataModel
{
    [ConfigurationName ("mru")]
    public class ItemNamePairLists : List<ItemNamePairList>
    {
        private static int Compare(ItemNamePairList list1, ItemNamePairList list2)
        {
            return -list1.LastUsed.CompareTo(list2.LastUsed);
        }
        public new void Sort()
        {
            base.Sort(Compare);
        }

        public static ItemNamePairLists Load()
        {
            ItemNamePairLists newList = ConfigurationHelper.Load<ItemNamePairLists>(true);
            newList.Sort();
            foreach (var itemlist in newList)
                itemlist.Owner = newList;
            return newList;
        }
        public void Save()
        {
            ConfigurationHelper.Save( this);
        }
        private void TrimToSize()
        {
            while (Count > Settings.Current.MRUMaxSize && Count > 0)
                RemoveAt(Count - 1);
        }
        public ItemNamePairList Add(ItemId[] items1, ItemId[] items2)
        {
            int count = 0;
            foreach (ItemId item in items1)
                if (!item.IsEmpty)
                {
                    count++;
                    break;
                }
            foreach (ItemId item in items2)
                if (!item.IsEmpty)
                {
                    count++;
                    break;
                }
            if (count == 0)
                return null;
            ItemNamePairList result;
            foreach (var list in this)
            {
                if (list.IsEqualTo(items1, items2))
                    return list;
            }
            Add(result = new ItemNamePairList(items1, items2));
            Sort();
            TrimToSize();
            
            Save();
            return result;
        }
        public void Select(ItemId[] items1, ItemId[] items2)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].IsEqualTo(items1, items2))
                {
                    if (i == 0)
                        return;
                    else
                    {
                        this[i].LastUsed = DateTime.Now;
                        Save();
                    }
                }
            }
            
        }

        public bool AddNew(ItemNamePairList newList)
        {
            ItemId[] items1 = newList.Items1;
            ItemId[] items2 = newList.Items2;
            
            foreach (var list in this)
            {
                if (list.IsEqualTo(items1, items2))
                {
                    list.Name = newList.Name;
                    list.ExplicitlyCreated = !string.IsNullOrEmpty(list.Name);
                    list.LastUsed = DateTime.Now;
                    return false;
                }
                if (list.Name == newList.Name && !String.IsNullOrEmpty(list.Name))
                {
                    list.Set(newList.Name , newList.Items1, newList.Items2);
                    return false;
                }

            }
            newList.LastUsed = DateTime.Now;
            this.Add(newList);
            this.Sort();
            
            TrimToSize();
            Save();
            return true;
        }
    }
}
